using System;
using System.Collections;
using System.Collections.Generic;
using Events;
using PrimeTween;
using Unity.VisualScripting;
using UnityEngine;

public class DifficultyCom : MonoBehaviour
{
   //
   [SerializeField] private VoidEventChannel GameStartFun;
   [SerializeField] private VoidEventChannel TutorialStartedVoidFun;//教学
   [SerializeField] private VoidEventChannel GameOverFun;
   
   private int minInx = 0;//小循环
   public int nowLevel = 0;//大循环
   private List<List<float>> nextEnemyTimeList;//敌人出现的时间
   private List<float> levelTimeList;//等级提升时间list
   private bool isGameStart = false;

   public float nextLevelTime = 0;
   public float lowTimeOver = 60;
   public float lowChangeTime = 8;
   public bool isLow = true;
   public int loopCnt = 1;
   public float GetOutEnemy()
   {
      if (minInx >= nextEnemyTimeList[nowLevel].Count)
      {
         minInx = 0;
      }

      float outTime = nextEnemyTimeList[nowLevel][minInx];
      minInx++;
      return outTime;
   }

   public void Awake()
   {
      GameStartFun.Raised += StartGame;
      TutorialStartedVoidFun.Raised += TutorialStart;
      GameOverFun.Raised += GameOver;
      nextEnemyTimeList = new List<List<float>>();
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[0].Add(1);
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[1] = new List<float>();
      nextEnemyTimeList[1].Add(1/loopCnt);
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[2].Add(0.480f/loopCnt);
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[3].Add(0.241f/loopCnt);
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[4].Add(0.241f/ (2 * loopCnt));
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[5].Add(0.241f);
      nextEnemyTimeList[5].Add(0.480f/(2 * loopCnt));
      nextEnemyTimeList[5].Add(0.241f);
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[6].Add(0.241f/2);
      nextEnemyTimeList[6].Add(0.241f/2);
      nextEnemyTimeList[6].Add(0.241f);
      nextEnemyTimeList[6].Add(0.241f/2);
      nextEnemyTimeList[6].Add(0.241f/4);
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[7].Add(0.241f);
      nextEnemyTimeList[7].Add(0.241f/ (2 * loopCnt));
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[8].Add(0.241f/ (2 * loopCnt));
      nextEnemyTimeList.Add(new List<float>());
      nextEnemyTimeList[9].Add(4);

      levelTimeList = new List<float>();
      levelTimeList.Add(8);//0
      levelTimeList.Add(8);//1
      levelTimeList.Add(16);//2
      levelTimeList.Add(32);//3
      levelTimeList.Add(1);//4
      levelTimeList.Add(8);//5
      levelTimeList.Add(16);//6
      levelTimeList.Add(30);//7
      levelTimeList.Add(2);//8
      levelTimeList.Add(4);//9
      
      nextLevelTime = levelTimeList[nowLevel];
   }

   public void OnDestroy()
   {
      GameStartFun.Raised -= StartGame;
      TutorialStartedVoidFun.Raised -= TutorialStart;
      GameOverFun.Raised -= GameOver;
   }

   public void StartGame()
   {
      nextLevelTime = levelTimeList[nowLevel];
      GameObject.Find("BgmNode").GetComponent<Animator>().Play("BgmStart",0,0);
      GameObject.Find("bgmBase").GetComponent<AudioLowPassFilter>().cutoffFrequency = 771;
      GameObject.Find("bgm").GetComponent<AudioLowPassFilter>().enabled = false;
      isGameStart = true;
   }

   public void TutorialStart()
   {
      GameObject.Find("BgmNode").GetComponent<Animator>().Play("TutorialStart",0,0);
      AudioLowPassFilter bgmBase = GameObject.Find("bgmBase").GetComponent<AudioLowPassFilter>();
      bgmBase.cutoffFrequency = 51;
      GameObject.Find("bgm").GetComponent<AudioLowPassFilter>().cutoffFrequency = 51;
      GameObject.Find("bgm").GetComponent<AudioLowPassFilter>().enabled = true;
   }

   public void GameOver()
   {
      GameObject.Find("BgmNode").GetComponent<Animator>().Play("Non",0,0);
      isGameStart = false;
   }

   public void Update()
   {
      if (!isGameStart)
      {
         return;
      }

      nextLevelTime -= Time.deltaTime;
      if (nextLevelTime <= 0)
      {
         nowLevel++;
         if (nowLevel >= levelTimeList.Count)
         {
            nowLevel = 0;
            loopCnt++;
            UpDataLevel();
            Debug.Log("1");
         }

         nextLevelTime = levelTimeList[nowLevel];
      }

      if (lowTimeOver <= 0)
      {
         lowTimeOver = 60;
         lowChangeTime = 0;
         isLow = !isLow;
         if (!isLow)
         {
            if (!(loopCnt > 2 && loopCnt % 2 == 0))
               GameObject.Find("bgmLong").GetComponent<AudioSource>().Stop();
         }

      }

      if (lowChangeTime < 8)
      {
         if (isLow)
         {
            GameObject.Find("bgmBase").GetComponent<AudioLowPassFilter>().cutoffFrequency =
               5000 - ((lowChangeTime / 8) * 4229);

         }
         else
         {
            GameObject.Find("bgmBase").GetComponent<AudioLowPassFilter>().cutoffFrequency =
               771 + ((lowChangeTime / 8) * 4229);
         }

         lowChangeTime += Time.deltaTime;
      }
      else if (lowChangeTime <= 21)
      {
         lowChangeTime += Time.deltaTime;
         if (isLow && lowChangeTime >= 21&&loopCnt>2)
         {
            {
               GameObject.Find("bgmLong").GetComponent<AudioSource>().Play();
            }
         }
      }
      
      lowTimeOver -= Time.deltaTime;
   }

   public void UpDataLevel()
   {
      nextEnemyTimeList[1][0] = 1f/loopCnt;
      nextEnemyTimeList[5][1]=(0.480f/(loopCnt));
      nextEnemyTimeList[7].Add(0.241f/ (loopCnt));
      nextEnemyTimeList[8].Add(0.241f/ loopCnt);
   }
}
